import { _decorator, AudioClip, Component, director, Node, Sprite, SpriteFrame, tween, v3 } from 'cc';
const { ccclass, property } = _decorator;

import CardFunctions from "../Common/CardFunctions";
import { Consts } from "../Data/Consts";
import { GameData } from "../Data/GameData";
import { ProductData } from "../Config/ProductData";
import { bdlMgr } from "../Manager/SS_BundleManager";
import { SS_AudioManager } from '../Manager/SS_AudioManager';
import { SS_Settings } from '../Data/SS_Settings';

@ccclass('WinPanel')
export default class WinPanel extends Component {
    public static Instance: WinPanel;
    @property(Node)
    public Button_nextStage: Node | null = null;
    @property(Node)
    public Button_wujin: Node | null = null;
    @property(Node)
    public unlock: Node | null = null;
    @property(Node)
    public item: Node | null = null;
    @property(Sprite)
    public unlock_name: Sprite | null = null;
    @property(Sprite)
    public unlock_icon: Sprite | null = null;
    @property(Node)
    public item_name: Node | null = null;
    @property(Node)
    public info: Node | null = null;
    @property(Sprite)
    public talk: Sprite | null = null;
    @property(Node)
    public 光线: Node | null = null;
    @property(Node)
    public 完美过关: Node | null = null;

    onLoad() {
        WinPanel.Instance = this;
    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }

    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }

    async Refresh() {
        if (GameData.currentUnlockId) {
            this.unlock.active = true;
            this.talk.node.active = true;
            this.完美过关.active = false;

            let item_data = ProductData.find(x => x.id == GameData.currentUnlockId);

            bdlMgr.getRes<SpriteFrame>(Consts.BundleName.SPRITE, `main/product/unlock_name/${item_data.icon}/spriteFrame`, SpriteFrame).then(res => { this.unlock_name.spriteFrame = res });
            bdlMgr.getRes<SpriteFrame>(Consts.BundleName.SPRITE, `main/product/icon/${item_data.icon}/spriteFrame`, SpriteFrame).then(res => { this.unlock_icon.spriteFrame = res });
            bdlMgr.getRes<SpriteFrame>(Consts.BundleName.SPRITE, `main/product/talk/${item_data.icon}/spriteFrame`, SpriteFrame).then(res => { this.talk.spriteFrame = res });

            this.item_name.active = false;
            this.info.active = false;
            this.item.setScale(0, 0, 0);
            tween(this.item)
                .to(0.5, { scale: v3(1, 1, 1) })
                .call(async () => {
                    this.item_name.active = true;
                    this.info.active = true;
                    this.光线.active = true;
                })
                .start();

            let talk = item_data.talk;

            bdlMgr.getRes<AudioClip>(Consts.BundleName.AUDIO, `配音/${talk}`, AudioClip).then(async (audio) => {
                SS_AudioManager.Instance.playOneShot(audio, SS_Settings.curMusicV);
            });
        }
        else {
            this.unlock.active = false;
            this.talk.node.active = false;

            this.完美过关.active = true;
            this.完美过关.setScale(0, 0, 0);
            tween(this.完美过关)
                .to(0.5, { scale: v3(1.5, 1.5, 1.5) })
                .start();

            bdlMgr.getRes<AudioClip>(Consts.BundleName.AUDIO, `sound/完美过关`, AudioClip).then(async (audio) => {
                SS_AudioManager.Instance.playOneShot(audio, SS_Settings.curMusicV);
            });
        }

        let 是否展示无尽按钮 = CardFunctions.获取当前关卡是否是无尽第1关(GameData.Get_CurrentLevel());

        this.Button_nextStage.active = !是否展示无尽按钮;
        this.Button_wujin.active = 是否展示无尽按钮;
    }

    AddListener() {
        this.Button_nextStage.on(Node.EventType.TOUCH_START, this.On_Button_nextStage, this);
        this.Button_wujin.on(Node.EventType.TOUCH_START, this.On_Button_nextStage, this);
    }
    RemoveListener() {
        this.Button_nextStage.off(Node.EventType.TOUCH_START, this.On_Button_nextStage, this);
        this.Button_wujin.off(Node.EventType.TOUCH_START, this.On_Button_nextStage, this);
    }

    On_Button_nextStage() {
        this.ShowOff();

        director.loadScene("GameScene");
    }
}